﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace DR5.SPExtend
{
    public static class SPListItemExtension
    {
        public static SPListItem AddTaxonomyEmpty(this SPListItem li, string colName)
        {
            li[colName] = string.Empty;
            return li;
        }

        public static SPListItem AddTaxonomyValue(this SPListItem li, string colName, TaxonomyFieldValue value)
        {
            TaxonomyField taxFld = li.Fields[colName] as TaxonomyField;
            taxFld.SetFieldValue(li, value);
            return li;
        }

        public static SPListItem AddValue(this SPListItem li, string colName, object value)
        {
            li[colName] = value;
            return li;
        }

        public static bool IsContentApproved(this SPListItem li)
        {
            if (li.ModerationInformation != null)
            {
                if (li.ModerationInformation.Status == SPModerationStatusType.Approved)
                    return true;
            }
            return false;
        }

        public static SPUser GetSPUser(this SPListItem spListItem, String fieldName)
        {
            SPUser spUser = null;
            try
            {
                if (fieldName != string.Empty)
                {
                    SPFieldUser field = spListItem.Fields[fieldName] as SPFieldUser;
                    if (field != null && spListItem[fieldName] != null)
                    {
                        SPFieldUserValue fieldValue = field.GetFieldValue(spListItem[fieldName].ToString()) as SPFieldUserValue;
                        if (fieldValue != null)
                        {
                            spUser = fieldValue.User;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return spUser;
        }

        public static SPUser[] GetSPUsers(this SPListItem item, string fldName)
        {
            SPUser[] userColl = null;
            if (item.Fields.ContainsField(fldName) && item[fldName] != null && item.Fields[fldName].Type == SPFieldType.User)
            {
                SPFieldUser field = item.Fields[fldName] as SPFieldUser;
                if (field.AllowMultipleValues == true)
                {
                    SPFieldUserValueCollection fieldValues = new SPFieldUserValueCollection(item.Web, item[fldName].ToString());
                    userColl = new SPUser[fieldValues.Count];
                    for (int i = 0; i < fieldValues.Count; i++)
                    {
                        SPFieldUserValue fieldUserValue = fieldValues[i];
                        userColl[i] = fieldUserValue.User;
                    }
                }
                else
                {
                    userColl = new SPUser[1];
                    SPFieldUserValue fieldValue = new SPFieldUserValue(item.Web, item[fldName].ToString());
                    userColl[0] = fieldValue.User;
                }
            }
            return userColl;
        }

        public static SPFieldUserValueCollection GetSPFieldUsers(this SPListItem item, string fieldName)
        {
            if (item[fieldName] == null)
            {
                return new SPFieldUserValueCollection(); ;
            }
            else
            {
                return new SPFieldUserValueCollection(item.Web, item[fieldName].ToString());
            }
        }

        public static string GetTermText(this SPListItem li, string fieldName)
        {
            string returnText = string.Empty;
            if (li[fieldName] != null)
            {
                returnText = li[fieldName].ToString();
                if (returnText.Contains('|'))
                {
                    returnText = returnText.Split('|')[0];
                }
            }
            return returnText;
        }

        public static string GetTermId(this SPListItem li, string fieldName)
        {
            string returnText = string.Empty;
            if (li[fieldName] != null)
            {
                returnText = li[fieldName].ToString();
                if (returnText.Contains('|'))
                {
                    returnText = returnText.Split('|')[1];
                }
            }
            return returnText;
        }

        public static string GetText(this SPListItem li, string fieldName)
        {
            string r = string.Empty;
            if (li[fieldName] != null)
            {
                r = li[fieldName].ToString();
            }
            return r;
        }

        public static bool GetYesOrNo(this SPListItem li, string fieldName)
        {
            if (li[fieldName] != null)
            {
                string liValue = li[fieldName].ToString();
                bool value = Convert.ToBoolean(liValue);
                return value;
            }
            return false;
        }

        public static string[] GetChoices(this SPListItem li, string fieldName)
        {
            SPFieldMultiChoiceValue choices = new SPFieldMultiChoiceValue(li[fieldName].ToString());
            string[] array = new string[choices.Count];
            for (int i = 0; i < choices.Count; i++)
            {
                array[i] = choices[i];
            }
            return array;
        }

        public static SPListItem Clone(this SPListItem li)
        {
            SPList list = li.ParentList;
            SPListItem newli = list.AddItem();
            foreach (SPField field in list.Fields)
            {
                //if (!(field.ReadOnlyField || field.Hidden))
                if ((!field.ReadOnlyField) && (field.InternalName != "Attachments"))
                {
                    newli[field.Id] = li[field.Id];
                }
            }
            newli.Update();
            return newli;
        }

        public static void UpdateWithSystemAccount(this SPListItem listItem, bool systemUpdate, params string[] fieldsName)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(listItem.Web.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb(listItem.Web.ID))
                    {
                        SPList list = web.Lists[listItem.ParentList.ID];
                        SPListItem li = list.GetItemById(listItem.ID);

                        foreach (string fieldName in fieldsName)
                        {
                            li[fieldName] = listItem[fieldName];
                        }
                        web.AllowUnsafeUpdates = true;
                        if (systemUpdate)
                        {
                            li.SystemUpdate(false);
                        }
                        else
                        {
                            li.Update();
                        }
                        web.AllowUnsafeUpdates = false;
                    }
                }
            });
        }
        
    }
}
